#!/usr/bin/env bash
#
# Find a container image that's compatible with the requested package(s),
# either locally, built from source, or pulled from DockerHub.
#
# For example, you can use this to automatically run a package without tracking
# down yourself exactly which container image/tag to run:
#
#   $ sudo docker run --runtime nvidia -it --rm $(autotag pytorch)
#   $ jetson-containers/run.sh $(autotag pytorch)   # shorthand for full 'docker run' command
#
# Or interspersed with more run arguments:
#
#   $ jetson-containers/run.sh --volume /my/dir:/mount $(autotag tensorflow2)
#   $ jetson-containers/run.sh --volume /my/dir:/mount $(autotag tensorflow2) /bin/bash -c 'some cmd'
#
# By default, the most-recent local image will be preferred - then DockerHub will be checked.
# If a compatible image isn't found on DockerHub, the user will be asked if they want to build it.
#
# See jetson_containers/tag.py for optional autotag arguments.
#
ROOT="$(dirname "$(readlink -f "$0")")"
PYTHONPATH="$PYTHONPATH:$ROOT" python3 -m jetson_containers.tag "$@" >&2

return_status=$?

if [ $return_status -ne 0 ]; then
	echo "-- Error:  return code $return_status" >&2
	exit $return_status
fi

cat /tmp/autotag

# this way didn't require a /tmp file, but couldn't have interactive input
#return_output=$(PYTHONPATH="$PYTHONPATH:$ROOT" python3 -m jetson_containers.tag "$@")
#return_status=$?

#printf "%s\n" "$return_output" >&2

#if [ $return_status -ne 0 ]; then
#	echo "-- Error:  return code $return_status" >&2
#	exit $return_status
#fi

#echo $(echo "$return_output" | tail -1)
